home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 5.9 KB | 203 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWCouPtr.h
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWCOUPTR_H
- #define FWCOUPTR_H
-
- #ifndef FWEXCLIB_H
- #include "FWExcLib.h"
- #endif
-
- //========================================================================================
- // CLASS FW_TCountedPtr<tRep>
- //========================================================================================
-
- template <class tRep>
- class FW_TCountedPtr
- {
- public:
- FW_DECLARE_AUTO(FW_TCountedPtr<tRep>)
-
- ~FW_TCountedPtr();
- // Destroy the pointer.
- // Decrements the count in the rep, and deletes the rep if count is zero.
-
- FW_TCountedPtr();
- // Creates a "NULL" pointer.
-
- FW_TCountedPtr(const FW_TCountedPtr<tRep>& other);
- // Point this pointer to the same rep as the other pointer points to.
-
- FW_TCountedPtr(tRep* rep);
- // Creates a pointer pointing at rep.
-
- FW_TCountedPtr<tRep>& operator=(const FW_TCountedPtr<tRep>& other);
- // Assignment, point this ponter to the same rep as the other pointer points to.
-
- FW_TCountedPtr<tRep>& operator=(tRep* rep);
- // Assignment, point this ponter to rep.
-
- tRep* operator->() const { return fRep; }
- // Provide access to members of rep.
-
- tRep& operator*() const { return *fRep; }
- // Provide access to rep.
- // This operator should be used only if operator->() is not sufficient.
-
- operator const void*() const;
- // Use to test if this is a "NULL" pointer.
-
- protected:
- void SetRep(tRep* rep);
-
- protected:
- tRep *fRep;
- };
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRep>::FW_TCountedPtr
- //----------------------------------------------------------------------------------------
-
- template <class tRep>
- inline FW_TCountedPtr<tRep>::FW_TCountedPtr() :
- fRep(NULL)
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRep>::~FW_TCountedPtr
- //----------------------------------------------------------------------------------------
-
- template <class tRep>
- inline FW_TCountedPtr<tRep>::~FW_TCountedPtr()
- {
- FW_START_DESTRUCTOR
- if (fRep && !fRep->DecrementReferenceCount()) delete fRep;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRep>::FW_TCountedPtr
- //----------------------------------------------------------------------------------------
-
- template <class tRep>
- inline FW_TCountedPtr<tRep>::FW_TCountedPtr(const FW_TCountedPtr& other) :
- fRep(other.fRep)
- {
- if (fRep) fRep->IncrementReferenceCount();
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRep>::FW_TCountedPtr
- //----------------------------------------------------------------------------------------
-
- template <class tRep>
- inline FW_TCountedPtr<tRep>::FW_TCountedPtr(tRep* rep) :
- fRep(rep)
- {
- if (fRep) fRep->IncrementReferenceCount();
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRep>::SetRep
- //----------------------------------------------------------------------------------------
-
- template <class tRep>
- inline void FW_TCountedPtr<tRep>::SetRep(tRep* rep)
- {
- if (fRep != rep)
- {
- if (fRep && !fRep->DecrementReferenceCount()) delete fRep;
- fRep = rep;
- if (fRep) fRep->IncrementReferenceCount();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRep>::operator=
- //----------------------------------------------------------------------------------------
-
- template <class tRep>
- inline FW_TCountedPtr<tRep>& FW_TCountedPtr<tRep>::operator=(const FW_TCountedPtr<tRep>& other)
- {
- SetRep(other.fRep);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRep>::operator=
- //----------------------------------------------------------------------------------------
-
- template <class tRep>
- inline FW_TCountedPtr<tRep>& FW_TCountedPtr<tRep>::operator=(tRep* rep)
- {
- SetRep(rep);
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_TCountedPtr<tRep>::operator=
- //----------------------------------------------------------------------------------------
-
- template <class tRep>
- inline FW_TCountedPtr<tRep>::operator const void*() const
- {
- return fRep;
- }
-
- //========================================================================================
- // CLASS FW_MCountedPtrRep
- //========================================================================================
-
- class FW_MCountedPtrRep
- {
- public:
-
- FW_MCountedPtrRep();
- virtual ~FW_MCountedPtrRep();
-
- long IncrementReferenceCount();
- long DecrementReferenceCount();
- long GetReferenceCount();
-
- private:
- long fRefCount;
- };
-
- //----------------------------------------------------------------------------------------
- // FW_MCountedPtrRep::DecrementReferenceCount
- //----------------------------------------------------------------------------------------
-
- inline long FW_MCountedPtrRep::DecrementReferenceCount()
- {
- return --fRefCount;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_MCountedPtrRep::IncrementReferenceCount
- //----------------------------------------------------------------------------------------
-
- inline long FW_MCountedPtrRep::IncrementReferenceCount()
- {
- return ++fRefCount;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_MCountedPtrRep::GetReferenceCount
- //----------------------------------------------------------------------------------------
-
- inline long FW_MCountedPtrRep::GetReferenceCount()
- {
- return fRefCount;
- }
-
- #endif
-